/**
 * 
 */
package com.whilefor.runner.modules.webapi.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import com.whilefor.runner.common.web.BaseController;

/**
 * @author D.linsen
 *
 */
@Controller
@RequestMapping(value = "/web/user")
public class UserRegisterController extends BaseController{
	
	@RequestMapping(value="/phone/register/{phoneNo}")
	@ResponseBody
	public Model sendRegisterSMS(@PathVariable String phoneNo, Model model, HttpSession session) {
		TaobaoClient client = new DefaultTaobaoClient("https://eco.taobao.com/router/rest", "23342161", "654f48db2c58d5a0b7675ce0746c6886");
		Double code = new Double(Math.random()*9000+1000);
		Map<String, String> pmap = new HashMap<String, String>();
		pmap.put("code", String.valueOf(code.intValue()));
		pmap.put("product", "runner.com");
		
		session.setAttribute(phoneNo, code);
		
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setSmsType("normal");
		req.setSmsFreeSignName("注册验证");
		req.setSmsParamString(JSON.toJSONString(pmap));
		req.setRecNum(phoneNo);
		req.setSmsTemplateCode("SMS_7435341");
		AlibabaAliqinFcSmsNumSendResponse rsp = null;
		try {
			rsp = client.execute(req);
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
		addMessage(model, "");
		return model;
	}
}
